import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';
import 'package:xindong/routes/call/controller/xindong_call_rtc_controller.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/container/xindong_drag_container.dart';
import 'package:xindong/widgets/xindong_avatar.dart';

// 悬浮  语音通话最小化
class XinDongCallMiniAudioDialog {
  // ======== 单例
  // 工厂方法构造函数
  factory XinDongCallMiniAudioDialog() => _getInstance();

  // instance的getter方法，singletonManager.instance获取对象
  static XinDongCallMiniAudioDialog get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static XinDongCallMiniAudioDialog? _instance;

  // 获取对象
  static XinDongCallMiniAudioDialog _getInstance() {
    _instance ??= XinDongCallMiniAudioDialog._internal();
    return _instance!;
  }

  // 私有的自定义命名式构造方法，通过它实现一个类;
  // 子类不能继承internal
  // 不是关键字，可定义其他名字
  XinDongCallMiniAudioDialog._internal() {
    //初始化(设置一些默认的)...
  }

  // 唯一标识
  final String tag = "kTagCallMinimizeAudio";

  Future<dynamic> show() async {
    return await SmartDialog.show(
      tag: tag,
      permanent: true,
      clickMaskDismiss: false,
      useAnimation: false,
      bindPage: false,
      maskColor: Colors.transparent,
      usePenetrate: true,
      keepSingle: true,
      builder: (BuildContext context) {
        return XinDongDragContainer(
          width: 60,
          height: 60,
          // offsetTop: (screenHeight - 60) / 2,
          child: GestureDetector(
            onTap: () => XinDongCallRTCController.to.showFullScreen(),
            behavior: HitTestBehavior.opaque,
            child: Stack(
              alignment: Alignment.center,
              children: [
                XinDongAvatarBorder(XinDongCallController.to.targetUser, avatarSize: 60.0),
                Container(
                  width: 56,
                  height: 56,
                  alignment: Alignment.center,
                  clipBehavior: Clip.antiAlias,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(32),
                    color: Colors.black26,
                  ),
                  child: ImageUtil.asset(AssetsRes.CALL_AUDIO, width: 20, height: 20),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  void hide() {
    try {
      SmartDialog.dismiss(status: SmartStatus.custom, tag: tag, force: true);
    } catch (error) {
      mCatchPrint(error);
    }
  }
}
